import { invmbInvmabVoList } from '@/api/url/base';
import { selectInvmaHeadVoTreeList } from '@/api/url/scheduling';
import { onMounted, reactive, watch, ref, computed } from 'vue';
import { VXETable } from 'vxe-table';
import XEClipboard from 'xe-clipboard';
import { getCustomProductIdByCustomId } from '@api/url/salesOrderManagement';
import { isNullPm } from '@/utils/baseMethods';

export default function importConfig() {
  const base = reactive<any>({
    queryInfo: null,
    id: null,
    maType: null,
    topTwo: null,
    name: null,
    shortcutCode: null,
    specification: null,
    quickQuery: null,
    baseDom: null,
    tabelDom: null,
    pagination: {
      current: 1,
      pageSize: 100,
      total: 0,
    },
    searchKey: '',
    originTreeData: [],
    tableRef: null,
    selectedRowKeys: [],
    selectionRows: [],
    type: '1',
    quickQueryTip: '请输入品名/品号/规格',
    result: false,
  });

  const menuConfig = reactive({
    className: 'my-menus',
    header: {
      options: [[{ code: 'exportAll', name: '导出所有.csv' }]],
    },
    body: {
      options: [
        [{ code: 'copy', name: '复制', prefixIcon: 'vxe-icon-copy', className: 'my-copy-item' }],
        [
          { code: 'remove', name: '删除', prefixIcon: 'vxe-icon-delete-fill color-red' },
          {
            name: '筛选',
            children: [
              { code: 'clearFilter', name: '清除筛选' },
              { code: 'filterSelect', name: '按所选单元格的值筛选' },
            ],
          },
          {
            code: 'sort',
            name: '排序',
            prefixIcon: 'vxe-icon-sort color-blue',
            children: [
              { code: 'clearSort', name: '清除排序' },
              { code: 'sortAsc', name: '升序', prefixIcon: 'vxe-icon-sort-alpha-asc color-orange' },
              { code: 'sortDesc', name: '倒序', prefixIcon: 'vxe-icon-sort-alpha-desc color-orange' },
            ],
          },
          { code: 'print', name: '打印', disabled: true },
        ],
      ],
    },
    footer: {
      options: [[{ code: 'clearAll', name: '清空数据' }]],
    },
  });

  const contextMenuClickEvent = ({ menu, row, column }) => {
    switch (menu.code) {
      case 'copy':
        // 示例
        if (row && column) {
          if (XEClipboard.copy(row[column.field])) {
            VXETable.modal.message({ content: '已复制到剪贴板！', status: 'success' });
          }
        }
        break;
      default:
        VXETable.modal.alert(`点击了 ${menu.name} 选项`);
    }
  };

  //获取表格数据
  const getTableData = () => {
    base.result = false;
    base.quickQuery = '';
    base.mg003 = '';
    base.mg005 = '';
    base.mg006 = '';

    if (base.queryInfo) {
      if (base.type == 1) {
        base.quickQuery = base.queryInfo;
      } else if (base.type == 2) {
        base.mg005 = base.queryInfo;
      } else if (base.type == 3) {
        base.mg003 = base.queryInfo;
      } else if (base.type == 4) {
        base.mg006 = base.queryInfo;
      }
    }

    const obj = {
      current: base.pagination.current,
      size: base.pagination.pageSize,
      customId: localStorage.getItem('customerId'),
    };
    const steam = {
      mg003: base.mg003,
      mg005: base.mg005,
      mg006: base.mg006,
      quickQuery: base.quickQuery,
    };

    getCustomProductIdByCustomId(obj, steam).then((res: any) => {
      if (res.code == '200') {
        res.data.list.forEach((item) => {
          item.id = item.mg003;
        });
        base.tabelDom = res.data.list;
        base.pagination.total = res.data.total;

        setTimeout(() => {
          getCheckedData();
        }, 0);
      }
    });
  };

  //存储选中数据
  const storeData = () => {
    const obj = {
      selectionRows: base.selectionRows,
      source: 'khph',
    };
    localStorage.setItem('customerNumData', JSON.stringify(obj));
  };

  //全选或取消全选
  const selectAllChangeEvent = ({ checked, records }) => {
    const steamRtem: any = localStorage.getItem('customerNumData');
    let steamOpem: any = null;
    if (!isNullPm(steamRtem)) steamOpem = JSON.parse(steamRtem).selectionRows;
    if (isNullPm(steamOpem)) steamOpem = [];
    if (!(base.pagination.current - 1 in steamOpem)) {
      steamOpem.push(records);
    } else {
      steamOpem[base.pagination.current - 1] = records;
    }
    base.selectionRows = steamOpem;
    storeData();
  };

  //复选框事件
  const selectChangeEvent = ({ records }) => {
    const steamRtem: any = localStorage.getItem('customerNumData');
    let steamOpem: any = null;
    if (!isNullPm(steamRtem)) steamOpem = JSON.parse(steamRtem).selectionRows;
    if (isNullPm(steamOpem)) steamOpem = [];
    if (!(base.pagination.current - 1 in steamOpem)) {
      steamOpem.push(records);
    } else {
      steamOpem[base.pagination.current - 1] = records;
    }
    base.selectionRows = steamOpem;
    storeData();
  };
  //获取选中元素
  const getCheckedData = () => {
    const steamRtem: any = localStorage.getItem('customerNumData');
    let steamOpem: any = null;
    if (!isNullPm(steamRtem)) {
      base.selectedRowKeys = [];
      steamOpem = JSON.parse(steamRtem);
      steamOpem.selectionRows?.map((res: any) => {
        res.map((reb: any) => {
          base.selectedRowKeys.push(reb.id);
        });
      });
      base.selectionRows = steamOpem.selectionRows;
    }
    base.result = true;
  };

  //修改查询条件的提示语
  const changeQueryTip = () => {
    if (base.type == 1) {
      base.quickQueryTip = '请输入品名/品号/规格';
    } else {
      base.quickQueryTip = '';
    }
  };

  onMounted(() => {
    getTableData();
  });

  return {
    base,
    menuConfig,
    contextMenuClickEvent,
    selectAllChangeEvent,
    selectChangeEvent,
    getTableData,
    changeQueryTip,
  };
}
